package srzy.data

import srzy.common.Config
import java.io.File
import srzy.common.Common
import srzy.dao.DBUtil

object PublicData extends srzy.common.Logging {
  //  var dbCompInfos = Map[String, List[srzy.RBTest.PublicData.DBCompInfo]]()
  //  //  if (Config.getValue("root", "config", "DBCompareFlag").toBoolean == true)
  //  dbCompInfos = DBOracle.getDBCompInfo
  val caseInfoSource = DBUtil("CaseInfoSource")
  //  val bureauSource = DBUtil("BureauSource")

  val dbCompInfos = caseInfoSource.getDBCompInfo
  logger.debug("dbCompInfos prepared")

  //  val dbBureauCompInfos = bureauSource.getDBCompInfo
  //  logger.debug("dbBureauCompInfos prepared")

  val caseInfo = caseInfoSource.getCaseInfo
  val caseInfoMap = caseInfo.toMap
  logger.debug("caseInfo prepared")

  //  var shellCompareInfos = Map[String, List[srzy.RBTest.PublicData.ShellCompInfo]]()
  //  //  if (Config.getValue("root", "config", "DBCompareFlag").toBoolean == true)
  //  shellCompareInfos = DBOracle.getShellCompInfo
  val shellCompareInfos = caseInfoSource.getShellCompInfo
  logger.debug("shellCompareInfos prepared")

  val fileName = Config.getValue("root", "config", "TestDataFileName")
  val caseInfos0 = if (fileName == "") {
    caseInfo.map(_._1)
  } else {
    Common.readFromFile("etc" + File.separator + Config.getValue("root", "config", "TestDataFileName"))
  }

  logger.debug("read case file : " + fileName)

  val caseInfos = caseInfos0
    .filter(x => if (x == "") false else true).zipWithIndex.map(strint => {
      val path = strint._1
      val id = strint._2
      logger.debug("start read file :" + path)
      val c = Case.casePath(path)
      val msgf = new File(c.getMsgPath)
      val f = new File(path)
      if (!f.isDirectory())
        throw new Exception("file:" + path + " is not a dict")

      val ci = new CaseInfo {
        caseId = id
        caseName = c.caseName
        casePath = c.getCasePath
        exceptCondition = caseInfoMap.getOrElse(c.getCasePath, "")
        sqlPath = c.getSqlPath
        msgPath = c.getMsgPath
      }

      ci.dbCompInfos = dbCompInfos.getOrElse(ci.caseName, List.empty[DBCompInfo])
      //      ci.dbBureauCompInfos = dbBureauCompInfos.getOrElse(ci.caseName, List.empty[DBCompInfo])
      ci.shellCompInfos = shellCompareInfos.getOrElse(ci.caseName, List.empty[ShellCompInfo])

      parseSQLFile(parseMsgFile(ci))
    })

  def parseMsgFile(ci: CaseInfo) = {
    val l = Common.readFromFile(ci.msgPath).filter(_ != "")
    val l1 = l.drop(2)
    if (l1.length % 2 != 0)
      throw new Exception("Msg File : " + ci.msgPath + " parse error")

    val l2 = l1.zipWithIndex.map(x => {
      val s = x._1
      val i = x._2
      if (i % 2 == 0) (i / 2, 'send, s) else (i / 2, 'receStd, s)
    }).groupBy(_._2)
    val send = l2.apply('send).sortBy(_._1).map(_._3)
    val receStd = l2.apply('receStd).sortBy(_._1).map(_._3)
    ci.sendMsg = send
    ci.receStdMsg = receStd
    ci
  }

  def parseSQLFile(ci: CaseInfo) = {
    val s = Common.readFromFile(ci.sqlPath)
    ci.initShell = s.mkString("\n")
    ci
  }

}